<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="row">
                <table class="layui-hide"  id="main-table" lay-filter="main-table"></table>
            </div>
        </div>
    </div>
</div>

<script th:inline="none"></script>
<script>
let questionId = '[[${questionId}]]';
let libId = '[[${libId}]]';
    layui.use(['table','form','element'], function(){
        var table = layui.table,
        element = layui.element,
            form = layui.form;
        table.render({
            elem: '#main-table'
            ,height: 440
            ,url: 'libChoice/getPageList'
            ,title: '选项列表'
            ,page: true
            ,toolbar: '#toolbar'
            ,where:{questionId :questionId}
            ,defaultToolbar:false
            ,cols: [
                [ 
                {type: 'checkbox'}
                ,{field: 'indexLetter',title: "索引",width:100}
                ,{field: 'text', title: "文本",templet:function(d){
                	return "<a class='colum-link' href='javascript:void(0)' onclick='edit(\""+d.id+"\",\"" + d.text + "\" , this)'>" + d.text + "</a>";
                }}
               
                ,{field: 'rightTxt',title: "是否正确",width:90}
                ,{field: 'answerText',title: "答案说明",width:290}
                ,{field: 'createTime',title: "创建时间",width:190}
            ]
            ]
            ,done:function(){
          	  enableRowClick(table,form,'main-table',"main-table");
             }
        });
        table.on('edit(main-table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
        });
       
        table.on('toolbar(main-table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id); 
            switch(obj.event){
                case 'add':
                    layer.open({
                        id:"openwin",
                        type: 2,
                        title:'增加选项',
                        area:['1000px','420px'],
                        content:  'rooter?url=ksxt/lib/add-choice&libId='+libId+"&libQuestionId="+questionId,
                        btn:['保存','关闭'],
                        yes:function(index,layero){
                            var dataForm = layer.getChildFrame('form', index);
                            dataForm.find("#submitBtn").click();
                            return false
                        }
                    });
                    break;
                case 'delete':
                    var data = checkStatus.data;
                    if(data.length>0){
                        var values = {};
                        var ids=[];
                        $.each(data,function(i,v){
                            ids.push(v.id);
                        })
                        values['ids'] = ids;
                        layer.confirm('确定要删除当前行么?', {icon: 3, title:'提示'}, function(index){
                        	$.post("libChoice/delete",values,function(res){
                        		if (res.code == 1) {
                                    layer.msg(res.msg);
                                } else {
                                    layer.close(index);
                                    table.reload('main-table');
                                    layer.msg("删除成功。");
                                }
                        	})
                        });
                    }else{
                        layer.msg("请选择操作数据!")
                    }
                    break;
                case 'edit':
                    var data2 = checkStatus.data;
                    if(data2.length>0){
                        edit(data2[0].id);
                    }else{
                        layer.msg("请选择操作数据！")
                    }
                    break;
            };
        });
        var $ = layui.$, active = {
            query: function(){

                table.reload("main-table",{where:{name:$("#name").val()}})
            },
            resetBtn:function () {
                $("#btReset").click();
                
            }
        };
        window.loadGrid = function(tableName){
            table.reload(tableName);
        }
        $('.layui-form .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
    function edit(id){
    	layer.open({
            id:"openwinc",
            type: 2,
            title:'修改选项信息',
            area:['1000px','420px'],
            content:  'rooter?url=ksxt/lib/add-choice&id='+id,
            btn:['保存','关闭'],
            yes:function(index,layero){
                var dataForm = layer.getChildFrame('form', index);
                dataForm.find("#submitBtn").click();
                return false
            }
        });
    }
    
</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add"><i class="layui-icon layui-icon-addition"></i>增加</button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>
    </div>
</script>
</body>
</html>